package com.health.service.impl;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.health.domain.dto.OrderDTO;
import com.health.domain.pojo.Member;
import com.health.domain.pojo.Order;
import com.health.domain.vo.OrderInfo;
import com.health.domain.vo.Result;
import com.health.mapper.MemberMapper;
import com.health.mapper.OrderMapper;
import com.health.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ghy
 * @version 1.0
 * @since 2025-06-24 16:22
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Integer> submit(OrderDTO orderDTO) {
        // 比对验证码
        String telephone = orderDTO.getTelephone();
        String validateCode = orderDTO.getValidateCode();
        String redisCode = stringRedisTemplate.opsForValue().get("member_order_code_" + telephone);
        if(!StrUtil.equals(validateCode, redisCode)) {
            throw new RuntimeException("验证码错误！");
        }
        // 通过手机号查询当前预约人是否存在
        Member member = memberMapper.selectByPhoneNumber(telephone);
        // 如果不存在，则直接注册成会员
        if(ObjUtil.isNull(member)) {
            member = new Member();
            member.setName(orderDTO.getName());
            member.setSex(orderDTO.getSex().toString());
            member.setIdCard(orderDTO.getIdCard());
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            // 从身份证号码中提取出生日：使用hutool工具类
            String birth = IdcardUtil.getBirth(orderDTO.getIdCard());
            Date birthday = null;
            try {
                birthday = new SimpleDateFormat("yyyyMMdd").parse(birth);
                member.setBirthday(birthday);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            memberMapper.insert(member);
        }
        // 创建预约信息
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDTO.getOrderDate());
        order.setOrderType("微信预约");
        order.setOrderStatus("未到诊");
        order.setSetmealId(orderDTO.getSetmealId());
        orderMapper.insert(order);
        // 返回预约单的ID
        return Result.success(order.getId());
    }

    @Override
    public Result<OrderInfo> findById(Integer id) {
        OrderInfo orderInfo = orderMapper.selectByIdWithMemberAndSetmeal(id);
        return Result.success(orderInfo);
    }
}
